// subpages0/stepPoint/stepIndex/stepIndex.js
const app = getApp();
import { request } from '../../utils/request.js'
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
    isAuth: true
  },

  onLoad: function (e) {
    var that = this;
    if (e && e.title) {
      app.setNavtitle(e.title);
      that.setData({
        title: e.title
      })
    } else {
      app.setNavtitle('微信步数');
    }
    app.setNavColor(that);
    if (that.data.navColor) {
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: that.data.navColor
      })
    }
    that.requestGoodslist();//积分商品
  },
  onShow: function () {
    var that = this;
    if (that.data.isAuth) {
      that.getStep();
    }
  },
  getauthSetting: function () {
    var that = this;
    wx.getSetting({
      success(res) {
        console.log(res.authSetting);
        if (res.authSetting['scope.werun'] == false) {
          wx.showModal({
            title: '提示',
            content: '为了更好的体验此功能,请允许获取您的微信运动权限',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.openSetting({
                  success(res) {
                    console.log(res.authSetting)
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '为了更好的体验此功能,请允许获取您的微信运动权限',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定');
                that.getStep();
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          })
        }
      }
    })
  },
  // 获取步数
  getStep: function () {
    var that = this;
    wx.showLoading({
      title: '获取中···'
    })
    wx.login({
      success: function (res) {
        if (res.code) {
          var code = res.code;
          wx.getWeRunData({
            success(res) {
              console.log('获取成功', res);
              // 拿 encryptedData 到开发者后台解密开放数据applet_step_get/applet_step_index
              var data = {};
              data.map = 'applet_step_index';
              data.code = code;
              data.encryptedData = res.encryptedData;
              data.iv = res.iv;
              if (data.encryptedData && data.iv) {
                that.getStepInfo(data);
              }
            },
            fail(res) {
              console.log('获取失败', res);
              wx.hideLoading();
              that.setData({
                isAuth: false
              })
              that.getauthSetting();
            },
            complete() {
              console.log('获取完成');
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  //得到步数信息
  getStepInfo: function (data) {
    var that = this;
    // wx.showLoading({
    //   title: '加载中',
    //   mask: true
    // })
    request({
      data: data,
      pointer: { page: this, fname: "getStep" },
    }).then(res => {
      console.log('测试封装requst步数相关信息', res.data.data);
      that.setData({
        stepInfo: res.data.data
      })
      app.globalData.stepRule = res.data.data.rule;
    }).catch(err => {
      console.log(err);
    })
  },
  //兑换积分
  toExchangePoint: function () {
    var that = this;
    var stepInfo = that.data.stepInfo;
    // if (!stepInfo.hadRecharge){
    //   wx.showToast({
    //     title: '您今天已经兑换过了',
    //     icon: "none"
    //   });
    //   return;
    // }
    wx.showLoading({
      title: '兑换中',
      mask: true
    })
    request({
      data: {
        map: 'applet_step_recharge',
        step: stepInfo.step
      },
      pointer: { page: this, fname: "toExchangePoint" },
    }).then(res => {
      console.log('测试封装requst步数相关信息', res.data.data);
      app.errorTip(that, res.data.data.msg, 2000);
      setTimeout(function () {
        that.onShow();
      }, 1000)
    }).catch(err => {
      console.log(err);
    })
  },
  requestGoodslist: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_community_points_goods';
    data.page = page;
    if (that.data.type) {
      data.type = that.data.type;
    }
    if (that.data.sid) {
      data.sid = that.data.sid;
    }
    if (that.data.flid) {
      data.flid = that.data.flid;
    }
    console.log(data);
    request({
      data: data,
      showTip: true,
      pointer: { page: this, fname: "requestGoodslist" },
    }).then(res => {
      console.log('测试封装requst步数积分商品', res.data.data);
      var allArr = [];
      var initArr = that.data.goodsList ? that.data.goodsList : '[]';
      var curArr = res.data.data;
      var lastPageLength = curArr.length;
      if (page > 0) {
        allArr = initArr.concat(curArr);
      } else {
        allArr = res.data.data;
      }
      page++;
      that.setData({
        goodsList: allArr,
        page: page
      })
      if (lastPageLength < 10) {
        that.setData({
          noMoretip: true,
          showLoading: false
        });
      }
      console.log(that.data.goodsList);
    }).catch(err => {
      console.log(err);
      if (page <= 0) {
        that.setData({
          goodsList: [],
          noMoretip: false,
          showLoading: false
        })
      } else {
        that.setData({
          noMoretip: true,
          showLoading: false
        });
      }
    })
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      isAuth: true
    });
    this.onShow();
    this.requestGoodslist();
    console.log("下拉刷新");
  },
  //排行榜
  toStepRank: function () {
    wx.navigateTo({
      url: '/subpages0/stepPoint/stepRank/stepRank',
    })
  },
  //兑换规则
  toStepRule: function () {
    wx.navigateTo({
      url: '/subpages0/stepPoint/stepRule/stepRule',
    })
  },
  //积分商品详情
  toJfGoodDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages/jfGoodDetail/jfGoodDetail?id=' + id,
    })
  },
  //积分商城
  toIntegralShop: function () {
    wx.navigateTo({
      url: '/subpages/integralShop/integralShop',
    })
  },
  //积分明细
  toPointDetail: function () {
    wx.navigateTo({
      url: '/subpages/integralDetails/integralDetails',
    })
  }
})